<?php

namespace App\Console\Commands;

use App\Events\RoomRegisterEvent;
use App\Http\Model\FinanceDiamond;
use App\Http\Services\MsgServices;
use App\Http\Services\UserServices;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CreateSmallRoom extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'createSmallRoom {userId=0}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '由于系统异常，导致小屋创建不成功，此命令可以为用户重新创建小屋';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
            echo "开启事务！\n";
            $userId = (int)$this->argument('userId')??0;
            if (empty($userId)){
                echo "非法用户ID！\n";
                DB::rollBack();
                return false;
            }
            $data = (new UserServices())->getResponseData($userId);
            if (empty($data)){
                echo "非法用户ID！\n";
                DB::rollBack();
                return false;
            }
            event(new RoomRegisterEvent($data, 1));
            DB::commit();
            echo "事务提交！\n";
        } catch (\Exception $e) {
            echo "创建小屋失败！\n";
            echo $e->getMessage();
            DB::rollBack();
        }

        return true;
    }
}